package Program;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import simulator.labyrinth.BarCode;
import simulator.labyrinth.Labyrinth;
import simulator.labyrinth.Line;
import simulator.labyrinth.Square;
import simulator.labyrinth.Wall;
import simulator.position.Point;
import simulator.position.Position;


public class LabyrinthParser 
{
	
	public static Labyrinth parse(File file) throws IOException
	{
		Labyrinth labyrinth = new Labyrinth();

		BufferedReader reader = new BufferedReader(new FileReader(file));
		
		reader.readLine();
//		String firstLine = reader.readLine();
//		String[] firstLineSplitted = firstLine.split(" ");
//		int cols = Integer.parseInt(firstLineSplitted[0]);
//		int rows = Integer.parseInt(firstLineSplitted[1]);
		
		String line = null;
		int rowNumber = 1;
		while( (line = reader.readLine()) != null)
		{
			if(line.length() > 0 && line.charAt(0) != '#')
			{
				line = line.replaceAll("\t+", "\t");	
				String[] lineSplit = line.split("\t");
				for(int col = 1; col <= lineSplit.length; col++)
					addSector(lineSplit[col - 1], labyrinth, rowNumber, col);
				rowNumber++;
			}
		}
		reader.close();
		return labyrinth;
	}
	
	public static Labyrinth parse(String filePath) throws IOException
	{
		return parse(new File(filePath));
	}
	
	private static void addSector(String sector, Labyrinth lab, int row, int col)
	{
		String[] parameters = sector.split("\\.");
		String type = parameters[0];
		String orientation = "";
		if(parameters.length >= 2)
			orientation = parameters[1];
		String barcode = "";
		if(parameters.length >= 3)
			barcode = parameters[2];

		/*
		Point botLeft 	= new Point((col - 1)*40, (row-1)*40);
		Point topLeft 	= new Point((col - 1)*40, row*40);
		Point topRight 	= new Point(col*40, row*40);
		Point botRight	= new Point(col*40, (row-1)*40);
		*/
		
		Point topLeft 	= new Point((col - 1)*40, (row-1)*40);
		Point botLeft 	= new Point((col - 1)*40, row*40);
		Point botRight 	= new Point(col*40, row*40);
		Point topRight	= new Point(col*40, (row-1)*40);
		
		if(type.equals("Straight"))
		{
			if(orientation.equals("N") || orientation.equals("S"))
			{
				lab.addWall(new Wall(botLeft, topLeft));
				lab.addWall(new Wall(botRight, topRight));
				lab.addLine( new Line(topLeft, topRight) );
				lab.addLine( new Line(botLeft, botRight) );
				int code = Integer.parseInt(barcode);
				lab.addBarcode(topLeft,new BarCode(code,new Position(topLeft.getPreciseX(),topLeft.getPreciseY(),0)));
			}
			if(orientation.equals("E") || orientation.equals("W"))
			{
				lab.addWall(new Wall(topLeft, topRight));
				lab.addWall(new Wall(botLeft, botRight));
				lab.addLine(new Line(botLeft, topLeft));
				lab.addLine(new Line(botRight, topRight));
				int code = Integer.parseInt(barcode);
				lab.addBarcode(topLeft,new BarCode(code,new Position(topLeft.getPreciseX(),topLeft.getPreciseY(),90)));
			}
		}
		
		if(type.equals("Corner"))
		{
			if(orientation.equals("N"))
			{
				lab.addWall(new Wall(botLeft, topLeft));
				lab.addWall(new Wall(topLeft, topRight));
				lab.addLine(new Line(botLeft, botRight));
				lab.addLine(new Line(botRight, topRight));
			}
			if(orientation.equals("E"))
			{
				lab.addWall(new Wall(botRight, topRight));
				lab.addWall(new Wall(topLeft, topRight));
				lab.addLine(new Line(botLeft, topLeft));
				lab.addLine(new Line(botLeft, botRight));
				
			}
			if(orientation.equals("S"))
			{
				lab.addWall(new Wall(botLeft, botRight));
				lab.addWall(new Wall(botRight, topRight));
				lab.addLine(new Line(topLeft, topRight));
				lab.addLine(new Line(botLeft, topLeft));
			}
			if(orientation.equals("W"))
			{
				lab.addWall(new Wall(botLeft, botRight));
				lab.addWall(new Wall(botLeft, topLeft));
				
				lab.addLine(new Line(topLeft, topRight));
				lab.addLine(new Line(botRight, topRight));
			}
		}
		
		if(type.equals("T"))
		{
			if(orientation.equals("N"))
			{
				lab.addWall(new Wall(topLeft, topRight));
				lab.addLine(new Line(botLeft, botRight));
				lab.addLine(new Line(botLeft, topLeft));
				lab.addLine(new Line(botRight, topRight));
			}
			if(orientation.equals("E"))
			{
				lab.addWall(new Wall(botRight, topRight));
				lab.addLine(new Line(topLeft, topRight));
				lab.addLine(new Line(botLeft, botRight));
				lab.addLine(new Line(botLeft, topLeft));
			}
			if(orientation.equals("S"))
			{
				lab.addWall(new Wall(botLeft, botRight));
				lab.addLine(new Line(topLeft, topRight));
				lab.addLine(new Line(botLeft, topLeft));
				lab.addLine(new Line(botRight, topRight));
			}
			if(orientation.equals("W"))
			{
				lab.addWall(new Wall(botLeft, topLeft));
				lab.addLine(new Line(topLeft, topRight));
				lab.addLine(new Line(botLeft, botRight));
				lab.addLine(new Line(botRight, topRight));
			}
		}
		
		if(type.equals("DeadEnd"))
		{
			if(orientation.equals("N"))
			{
				lab.addWall(new Wall(topLeft, topRight));
				lab.addWall(new Wall(botLeft, topLeft));
				lab.addWall(new Wall(botRight, topRight));
				lab.addLine(new Line(botLeft, botRight));
			}
			if(orientation.equals("E"))
			{
				lab.addWall(new Wall(botLeft, botRight));
				lab.addWall(new Wall(topLeft, topRight));
				lab.addWall(new Wall(botRight, topRight));
				lab.addLine(new Line(botLeft, topLeft));
			}
			if(orientation.equals("S"))
			{
				lab.addWall(new Wall(botLeft, botRight));
				lab.addWall(new Wall(botLeft, topLeft));
				lab.addWall(new Wall(botRight, topRight));
				lab.addLine(new Line(topLeft, topRight));
			}
			if(orientation.equals("W"))
			{
				lab.addWall(new Wall(botLeft, botRight));
				lab.addWall(new Wall(topLeft, topRight));
				lab.addWall(new Wall(botLeft, topLeft));
				lab.addLine(new Line(botRight, topRight));
			}
		}
		
		if(type.equals("Cross"))
		{
			lab.addSquare(topLeft, new Square());
			lab.addLine(new Line(topLeft, topRight));
			lab.addLine(new Line(botLeft, botRight));
			lab.addLine(new Line(botLeft, topLeft));
			lab.addLine(new Line(botRight, topRight));
		}
		
	}
	
}
